package com.excilys.formation.project.views;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.joda.time.format.DateTimeFormat;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.excilys.formation.project.entities.Client;
import com.excilys.formation.project.entities.Operation;

@Component
public class ExcelView extends AbstractExcelView {

    public ExcelView() {

        this.setUrl("/resources/OperationTemplate");

    }

    @SuppressWarnings("unchecked")
    @Override
    protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        List<Operation> compteData = (List<Operation>) model.get("compteData");
        Client client = (Client) model.get("client");

        HSSFSheet sheet = workbook.getSheetAt(0);

        HSSFRow headerrow = sheet.createRow(7);
        headerrow.createCell(4).setCellValue(client.getNom() + " " + client.getPrenom());

        int rowNum = 13;
        for (Operation operation : compteData) {
            // create the row data

            HSSFRow row = sheet.getRow(rowNum++);
            row.getCell(1).setCellValue(operation.getTypeOperation().getLibelle());
            row.getCell(2).setCellValue(DateTimeFormat.forStyle("LS").withLocale(Locale.FRANCE).print(operation.getTransaction().getDate()));

            row.getCell(3).setCellValue(operation.getTransaction().getLibelle().toString().toString());
            row.getCell(4).setCellValue(operation.getMontant());
        }

    }
}
